//
//  WRCustomNavigationBar.swift
//  WRNavigationBar_swift
//
//  Created by itwangrui on 2017/11/25.
//  Copyright © 2017年 wangrui. All rights reserved.
//

import UIKit

fileprivate let WRDefaultTitleSize:CGFloat = 18
fileprivate let WRDefaultTitleColor = UIColor.init(hex: 0x000000)
fileprivate let WRDefaultBackgroundColor = UIColor.white
fileprivate let WRScreenWidth = UIScreen.main.bounds.size.width


// MARK: - Router
extension UIViewController
{
    //  A页面 弹出 登录页面B
    //  presentedViewController:    A页面
    //  presentingViewController:   B页面
    
    func wr_toLastViewController(animated:Bool)
    {
        if self.navigationController != nil
        {
            if self.navigationController?.viewControllers.count == 1
            {
                self.dismiss(animated: animated, completion: nil)
            } else {
                self.navigationController?.popViewController(animated: animated)
            }
        }
        else if self.presentingViewController != nil {
            self.dismiss(animated: animated, completion: nil)
        }
    }
    
    class func wr_currentViewController() -> UIViewController
    {
        if let rootVC = UIApplication.shared.delegate?.window??.rootViewController {
            return self.wr_currentViewController(from: rootVC)
        } else {
            return UIViewController()
        }
    }

    class func wr_currentViewController(from fromVC:UIViewController) -> UIViewController
    {
        if fromVC.isKind(of: UINavigationController.self) {
            let navigationController = fromVC as! UINavigationController
            return wr_currentViewController(from: navigationController.viewControllers.last!)
        }
        else if fromVC.isKind(of: UITabBarController.self) {
            let tabBarController = fromVC as! UITabBarController
            return wr_currentViewController(from: tabBarController.selectedViewController!)
        }
        else if fromVC.presentedViewController != nil {
            if fromVC.presentingViewController == nil {
                return fromVC
            }else{
                
            }
            return wr_currentViewController(from:fromVC.presentingViewController!)
        }
        else {
            return fromVC
        }
    }
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////
class WRCustomNavigationBar: UIView
{
    var pageBackCall:(()->())?
    var onClickLeftButton:(()->())?
    var onClickRightButton:(()->())?
    var rightNaviCustomView = UIView()
    var title:String? {
        willSet {
            titleLabel.isHidden = false
            titleLabel.text = newValue
        }
    }
    var titleLabelColor:UIColor? {
        willSet {
            titleLabel.textColor = newValue
        }
    }
    var titleLabelFont:UIFont? {
        willSet {
            titleLabel.font = newValue
        }
    }
    var barBackgroundColor:UIColor? {
        willSet {
            backgroundImageView.isHidden = true
            backgroundView.isHidden = false
            backgroundView.backgroundColor = newValue
        }
    }
    var barBackgroundImage:UIImage? {
        willSet {
            backgroundView.isHidden = true
            backgroundImageView.isHidden = false
            backgroundImageView.image = newValue
        }
    }
    
    // fileprivate UI variable
     var titleLabel:UILabel = {
         let label = UILabel()
         label.textColor = WRDefaultTitleColor
         label.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
         label.textAlignment = .left
         label.isHidden = true
         return label
     }()
    
    public lazy var leftButton:UIButton = {
        let button = UIButton()
        button.imageView?.contentMode = .center
        button.isHidden = true
        button.addTarget(self, action: #selector(clickBack), for: .touchUpInside)
        return button
    }()
    
    var rightButton:UIButton = {
        let button = UIButton()
        button.imageView?.contentMode = .center
        button.isHidden = true
        button.addTarget(self, action: #selector(clickRight), for: .touchUpInside)
        return button
    }()
    
    fileprivate lazy var bottomLine:UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.init(hex: 0xeeeeee)
        return view
    }()
    
    fileprivate lazy var backgroundView:UIView = {
        let view = UIView()
        return view
    }()
    
    fileprivate lazy var backgroundImageView:UIImageView = {
        let imgView = UIImageView()
        imgView.isHidden = true
        return imgView
    }()
    
    // fileprivate other variable
    fileprivate static var isIphoneX:Bool {
        get {
            if #available(iOS 11, *) {
                if let bottom = x_window?.safeAreaInsets.bottom, bottom > 0 {
                    return true
                }
            }
            return false
        }
    }
    
    fileprivate static var x_window: UIWindow? {
        get {
            if let app = UIApplication.shared.delegate as? AppDelegate {
                return app.window
            }
            return nil
        }
    }
    
    fileprivate static var navBarBottom: CGFloat {
        get {
            return NavBarHight
        }
    }
    
    // init
    class func CustomNavigationBar() -> WRCustomNavigationBar {
        let frame = CGRect(x: 0, y: 0, width: WRScreenWidth, height:navBarBottom)
        return WRCustomNavigationBar(frame: frame)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        setupView()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupView()
    }
    
    func setupView()
    {
        addSubview(backgroundView)
        addSubview(backgroundImageView)
        addSubview(leftButton)
        addSubview(titleLabel)
        
        if self.rightNaviCustomView.frame.size.width > 0 {
            addSubview(self.rightNaviCustomView)
        }else{
            addSubview(rightButton)
        }
        addSubview(bottomLine)
        updateFrame()
        backgroundColor = UIColor.clear
        backgroundView.backgroundColor = WRDefaultBackgroundColor
    }
    func updateFrame()
    {
        let top:CGFloat = IphoneTopArea //WRCustomNavigationBar.isIphoneX ? 44 : 22
        let margin:CGFloat = 0
        let buttonHeight:CGFloat = 44
        let buttonWidth:CGFloat = 44
        let titleLabelHeight:CGFloat = 44
        let titleLabelWidth:CGFloat = 200
        
        backgroundView.frame = self.bounds
        backgroundImageView.frame = self.bounds
        leftButton.frame = CGRect(x: margin, y: top, width: buttonWidth, height: buttonHeight)
        rightButton.frame = CGRect(x: WRScreenWidth-buttonWidth-margin-20, y: top, width: buttonWidth+20, height: buttonHeight)
        titleLabel.frame = CGRect(x: leftButton.right, y: top, width: titleLabelWidth, height: titleLabelHeight)
        bottomLine.frame = CGRect(x: 0, y: bounds.height-0.5, width: WRScreenWidth, height: 0.5)
    }
    
    
}


extension WRCustomNavigationBar
{
    func wr_setBottomLineHidden(hidden:Bool) {
        bottomLine.isHidden = hidden
    }
    func wr_setBackgroundAlpha(alpha:CGFloat) {
        backgroundView.alpha = alpha
        backgroundImageView.alpha = alpha
        bottomLine.alpha = alpha
    }
    func wr_setTintColor(color:UIColor) {
        leftButton.setTitleColor(color, for: .normal)
        rightButton.setTitleColor(color, for: .normal)
        titleLabel.textColor = color
    }
    
    // 左右按钮共有方法
    func wr_setLeftButton(normal:UIImage, highlighted:UIImage) {
        wr_setLeftButton(normal: normal, highlighted: highlighted, title: nil, titleColor: nil)
    }
    func wr_setLeftButton(image:UIImage) {
        wr_setLeftButton(normal: image, highlighted: image, title: nil, titleColor: nil)
    }
    func wr_setLeftButton(title:String, titleColor:UIColor) {
        wr_setLeftButton(normal: nil, highlighted: nil, title: title, titleColor: titleColor)
    }
    
    func wr_setRightButton(normal:UIImage, highlighted:UIImage) {
        wr_setRightButton(normal: normal, highlighted: highlighted, title: nil, titleColor: nil)
    }
    func wr_setRightButton(image:UIImage) {
        wr_setRightButton(normal: image, highlighted: image, title: nil, titleColor: nil)
    }
    func wr_setRightButton(title:String, titleColor:UIColor) {
        wr_setRightButton(normal: nil, highlighted: nil, title: title, titleColor: titleColor)
    }
    
    
    // 左右按钮私有方法
    private func wr_setLeftButton(normal:UIImage?, highlighted:UIImage?, title:String?, titleColor:UIColor?) {
        leftButton.isHidden = false
        leftButton.setImage(normal, for: .normal)
        leftButton.setImage(highlighted, for: .highlighted)
        leftButton.setTitle(title, for: .normal)
        leftButton.setTitleColor(titleColor, for: .normal)
    }
    func wr_setRightButton(normal:UIImage?, highlighted:UIImage?, title:String?, titleColor:UIColor?) {
        rightButton.isHidden = false
        rightButton.setImage(normal, for: .normal)
        rightButton.setImage(highlighted, for: .highlighted)
        rightButton.setTitle(title, for: .normal)
        rightButton.setTitleColor(titleColor, for: .normal)
    }
    
    func leftBtnClickAction()  {
        //
    }
  
   
}


// MARK: - 导航栏左右按钮事件
extension WRCustomNavigationBar
{
    @objc func clickBack() {
        if let onClickBack = onClickLeftButton {
            onClickBack()
            self.leftBtnClickAction()
        } else {
            let currentVC = UIViewController.wr_currentViewController()
            currentVC.wr_toLastViewController(animated: true)
        }
        
        
    }
    @objc func clickRight() {
        if let onClickRight = onClickRightButton {
            onClickRight()
        }
    }
}
























